{include file="public/header" /}



<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3>问题管理</h3>
                <h5></h5>
            </div>
            {include file="public/admin_items" /}
        </div>
    </div>
    <div class="fixed-empty"></div>

    <form method="get" name="formSearch" id="formSearch">
        <div class="ds-search-form">
            <dl>
                <dt>问题分类</dt>
                <dd><input type="text" value="{$Request.param.title}" name="title" class="txt"></dd>
            </dl>
            <dl>
                <dt>分类</dt>
                <select name="search_gc_id1" id="search_gc_id1">
                    <option value="">{$Think.lang.ds_please_choose}</option>
                    {notemtpy name="$class_list"}
                    {foreach $class_list as $key => $val}
                    <option value="{$val.gc_id}" {$Request.param.search_gc_id1 == $val.gc_id ? 'selected' : ''}>{$val.gc_name}</option>
                    {/foreach}
                    {/notempty}
                </select>
                <select name="search_gc_id2" id="search_gc_id2">
                    <option value="">{$Think.lang.ds_please_choose}</option>
                </select>
                <select name="search_gc_id3" id="search_gc_id3">
                    <option value="">{$Think.lang.ds_please_choose}</option>
                </select>
            </dl>
            <dl>
                <dt>状态</dt>
                <dd>
                    <select name="search_status" id="">
                        <option value="">全部</option>
                        <option value="1" {$Request.param.search_status == 1 ? 'selected' : ''}>启用</option>
                        <option value="2" {$Request.param.search_status == 2 ? 'selected' : ''}>停用</option>
                    </select>
                </dd>
                <dd><input type="submit" class="btn btn-small" value="{$Think.lang.ds_search}"></dd>
                <dd><input type="button" id="celsearch" class="btn btn-small" value="取消搜索"></dd>
                <script>
                    // todo 取消搜索
                    $("#celsearch").click(function (){
                        $("[name='title']").val('');
                        $("[name='search_status']").val('');
                        $("[name='search_gc_id1']").val('');
                        $("[name='search_gc_id2']").val('');
                        $("[name='search_gc_id3']").val('');
                        $('#formSearch').submit()
                    })
                </script>
            </dl>

            <div class="btn_group">
    <!--                <input type="submit" class="btn" value="{$Think.lang.ds_search}">-->
            </div>
        </div>
    </form>
<!--    <div class="ds-search-form">-->

<!--    </div>-->

    <table class="ds-default-table">
        <thead>
        <tr class="thead">
            <th class="w48">序号</th>
            <th>{$Think.lang.question_index_name}</th>
<!--            <th>状态</th>-->
            <th>分类</th>
            <th>添加时间</th>
            <th class="w300 align-center">{$Think.lang.ds_handle}</th>
        </tr>
        <tbody>
        {notempty name="question_list"}
        {foreach name="question_list" item="question"}
        <tr id="ds_row_{$question.id}">
            <td>{$question.id}</td>
            <td>{$question.title}</td>
            <td>{$questionModel::getCategoryName($question->gc_id1)} > {$questionModel::getCategoryName($question->gc_id2)} > {$questionModel::getCategoryName($question->gc_id3)}</td>

            <td>{$question.created_at}</td>
<!--            <td>-->
<!--                <button type="button" data-id="{$question.id}" data-status="{$question.status}" data-tishi="{$question.status == 1 ? '停用' : '启用'}" onclick="stopQuestion($(this))" class="btn btn-small {$question.status == 1 ? '' : 'btn-red'}" title="{$question.status == 1 ? '点击停用' : '点击启用'}">{$question.status == 1 ? '启用' : '停用'}</button>-->
<!--            </td>-->
            <td>
                <a href="{:url('Question/edit',['question_id'=>$question.id])}"  class="dsui-btn-edit"><i class="iconfont"></i>{$Think.lang.ds_edit}</a>
                <a type="button" data-id="{$question.id}" data-status="{$question.status}" data-tishi="{$question.status == 1 ? '停用' : '启用'}" onclick="stopQuestion($(this))" class="dsui-btn-edit {$question.status == 1 ? '' : 'dsui-btn-del'}" title="{$question.status == 1 ? '点击停用' : '点击启用'}">{$question.status == 1 ? '启用' : '停用'}</a>

                <!--                <a href="javascript:dsLayerOpen('{:url('Question/option',['question_id'=>$question.id])}','选项-管理')"  class="dsui-btn-edit"><i class="iconfont"></i>选项管理</a>-->
<!--                <a href="javascript:dsLayerOpen('{:url('Question/addChild',['parent_id'=>$question.id])}','{$Think.lang.ds_add}-{$question.title}下级问题')"  class="dsui-btn-edit"><i class="iconfont"></i>添加子类问题</a>-->
<!--                <a href="javascript:dsLayerOpen('{:url('Question/addAnswer',['question_id'=>$question.id])}','{$Think.lang.ds_edit}-{$question.title}的答案')"" class="dsui-btn-edit"><i class="iconfont"></i>添加答案</a>-->
                <a href="javascript:void(0);" onclick="submit_delete('{$question->id}')"  class="dsui-btn-del"><i class="iconfont"></i>{$Think.lang.ds_del}</a>
            </td>
        </tr>
        {/foreach}
        {else /}
        <tr class="no_data">
            <td colspan="10">{$Think.lang.ds_no_record}</td>
        </tr>
        {/notempty}
        </thead>
    </table>
    {$show_page}
</div>
<script src="{$Think.ADMIN_SITE_ROOT}/js/question.js"></script>
<script type="text/javascript" src="{$Think.ADMIN_SITE_ROOT}/js/jquery.edit.js" charset="utf-8"></script>
<script type="text/javascript">
    function stopQuestion(that) {
        var msg = that.attr('data-tishi');
        var id = that.attr('data-id');
        var status = that.attr('data-status')
        layer.confirm('确定要'+msg+'吗', {
            btn: ['确定', '取消'],
            title: false,
        }, function () {
            $.post(ADMINSITEURL + "/Question/stopQuestion.html?id="+id, {}, function (res) {
                console.log(res);
                layer.msg(res.message);
                if (res.code != 10000) {
                    return false;
                }
                if (status == 1) {
                    that.addClass('dsui-btn-del');
                    status = 0;
                    msg = '停用';
                    var tmsg = '启用';
                    var tmsg = '启用';
                } else {
                    that.removeClass('dsui-btn-del');
                    status = 1;
                    msg = '启用';
                    var tmsg = '停用';
                }
                that.attr('data-status', status);
                that.attr('data-tishi', tmsg)
                that.attr('title', '点击'+tmsg)
                that.text(msg);
                layer.closeAll();
            })
        })
    }
    function submit_delete(id){
        layer.confirm('确定要删除该项吗？', {
            btn: ['确定', '取消'],
            title: false,
        }, function () {
            _uri = ADMINSITEURL + "/Question/delete.html?id=" + id;
            $.get(_uri, function (data) {
                data = JSON.parse(data);
                console.log(data)
                layer.msg(data.message, {time: 1000}, function () {
                    if (data.code == 10000) {
                        $("#ds_row_" + id).remove();
                    }
                })
            }, 'json')
        })
    }

    function getGcId1(gc_id1, select_id)
    {
        var select_od = select_id;
        $.ajax({
            type : "GET",
            contentType: "application/json;charset=UTF-8",
            url : ADMINSITEURL+"/Question/getClass.html?gc_id=" + gc_id1,
            success : function(result) {
                result = JSON.parse( result );
                var html = '<option value="">选择分类</option>';
                for(var i=0;i<result.length;i++){
                    if (result[i].gc_id == select_id) {
                        html += '<option selected value="'+ result[i].gc_id +'">'+ result[i].gc_name +'</option>'
                    } else {
                        html += '<option value="'+ result[i].gc_id +'">'+ result[i].gc_name +'</option>'
                    }
                }
                $('#search_gc_id2').html(html)
            },
            //请求失败，包含具体的错误信息
            error : function(e){
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }

    function getGcId2(gc_id1, select_id)
    {
        $.ajax({
            type : "GET",
            contentType: "application/json;charset=UTF-8",
            url : ADMINSITEURL+"/Question/getClass.html?gc_id=" + gc_id1,
            success : function(result) {
                result = JSON.parse( result );
                var html = '<option value="">选择分类</option>';
                for(var i=0;i<result.length;i++){
                    if (result[i].gc_id == select_id) {
                        html += '<option selected value="'+ result[i].gc_id +'">'+ result[i].gc_name +'</option>'
                    } else {
                        html += '<option value="'+ result[i].gc_id +'">'+ result[i].gc_name +'</option>'
                    }
                }
                $('#search_gc_id3').html(html)
            },
            //请求失败，包含具体的错误信息
            error : function(e){
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }
    //获取下级分类
    $('#search_gc_id1').on('change',function () {
        var gc_id1 = $(this).val();
        getGcId1(gc_id1)
    })

    $('#search_gc_id2').on('change',function () {
        var gc_id2 = $(this).val();
        getGcId2(gc_id2)
    })
</script>
{if $Request.param.search_gc_id1}
<script>

    // 编辑初始化，处理分类选中的问题
        var gc_id1 = '{$Request.param.search_gc_id1}';
        var gc_id2 = '{$Request.param.search_gc_id2}';
        var gc_id3 = '{$Request.param.search_gc_id3}';
        getGcId1(gc_id1, gc_id2)
        getGcId2(gc_id2, gc_id3)
</script>
{/if}
